import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/index/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
    meta: {
      title: 'Gmatrixs',
      keepAlive: false,
    }
  },
  {
    path: '/downloads',
    name: 'Dawnloads',
    // route level code-splitting
    // this generates a separate chunk (dawnloads.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "dawnloads" */ '../views/dawnloads/Dawnloads.vue'),
    meta: {
      title: 'Downloads',
      keepAlive: false,
    }
  },
  {
    path: '/news',
    name: 'News',
    // route level code-splitting
    // this generates a separate chunk (news.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "news" */ '../views/news/News.vue'),
    meta: {
      title: 'News',
      keepAlive: true,
    }
  },
  {
    path: '/newsDetail',
    name: 'NewsDetail',
    // route level code-splitting
    // this generates a separate chunk (newsDetail.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "newsDetail" */ '../views/news/NewsDetail.vue'),
    meta: {
      title: 'NewsDetail',
      keepAlive: false,
    }
  },
  {
    path: '/byToken',
    name: 'ByToken',
    // route level code-splitting
    // this generates a separate chunk (byToken.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "byToken" */ '../views/byToken/ByToken.vue'),
    meta: {
      title: 'By Token',
      keepAlive: false,
    }
  },
  {
    path: '/games',
    name: 'GAMES',
    // route level code-splitting
    // this generates a separate chunk (application.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "application" */ '../views/application/Application.vue'),
    meta: {
      title: 'GAMES',
      keepAlive: true,
    }
  },
  {
    path: '/gameDetail',
    name: 'GameDetail',
    // route level code-splitting
    // this generates a separate chunk (applicationDetail.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "applicationDetail" */ '../views/application/ApplicationDetail.vue'),
    meta: {
      title: 'GameDetail',
      keepAlive: false,
    }
  },
  {
    path: '*',
    name: '/404',
    component: resolve => require(['../views/404/404.vue'], resolve),
    meta: {
      title: 'error',
    }
  },
  // {
  //   path: '/test',
  //   name: 'Test',
  //   // route level code-splitting
  //   // this generates a separate chunk (test.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "test" */ '../views/games/Games.vue'),
  //   meta: {
  //     title: 'Test'
  //   }
  // }

]

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next()
})
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
})


export default router
